// TrainUploadLearningRecord.js
	var uploadCourseTimer;
	var uploadChapterTimer;
	var delayTime = 5 * 60 * 60 * 1000;
	var uid;
	function setupUploadRecord(){
		prepareData();
		api.addEventListener({
		    name: 'stopUploadNotifi'
		}, function(ret, err){
			stopCourseTimer();
		});

		// 课程章节学习记录上传 
		api.addEventListener({
		    name: 'uploadCourseRecordNotifi'
		}, function(ret, err){

			// uploadCourseTimer = setInterval(function(){
                searchUploadCourseFailedRecord();
   //          },60000);
		});
		// 章节学习详情记录上传
		api.addEventListener({
		    name: 'uploadChapterRecordNotifi'
		}, function(ret, err){
		    // startChapterTimer();
		    // alert('开始上传学习详情');
		    searchUploadChapterFailedRecord();
		});

		searchUploadCourseFailedRecord();
		searchUploadChapterFailedRecord();
		searchUploadTestAnswerFailed();
		//课时学习点返回时调用
		api.addEventListener({
		    name: 'courseStudiedFinishNotifi'
		}, function(ret, err){
			// alert('课程学完开始上传学习详情');
		    searchUploadChapterFailedRecord();
		});
		api.addEventListener({
		    name:'online'
		}, function(ret, err){        
		    if( ret ){
		        searchUploadCourseFailedRecord();
				searchUploadChapterFailedRecord();
				searchUploadTestAnswerFailed();
				
		    }
		});
	}
	function startCourseTimer(){
		uploadCourseTimer = setInterval(function(){
			searchUploadCourseFailedRecord();
		}, delayTime);
	}
	function stopCourseTimer(){
		clearInterval(uploadCourseTimer);
	}
	function startChapterTimer(){
		uploadChapterTimer = setInterval(function(){
			searchUploadChapterFailedRecord();
		}, delayTime);
	}
	function stopChapterTimer(){
		clearInterval(uploadChapterTimer);
	}
	function uploadCourseRecord(params, type, callback){
		var requestUrl = "";
		if (type == 'course_record') {
			// requestUrl = study_course_upload_record;
			requestUrl = study_course_offstudyins;
		}else if(type == 'chapter_record'){
			requestUrl = study_course_upload_record_detail;
		}
		api.ajax({
		    url: requestUrl,
		    method: 'post',
		    timeout: 30,
		    dataType: 'json',
		    returnAll: false,
		    data: params
		},function(ret,err){
		    if (ret) {
		    	// alert(JSON.stringify(ret));
		        
		        if (type == 'chapter_record') {
		        	callback(params.values);
		        }else{
		        	if (ret.status == '200') {
			        	callback(params.values);
			        }
		        }
		    } else {
//		        api.toast({
//		            msg: JSON.stringify(err),
//		            duration: 4000,
//		            location: 'bottom'
//		        });
		    };
		});
	}
	function searchUploadCourseFailedRecord(){

		openDB(function(ret, err){
			if (ret.status) {
				var sql = "SELECT * FROM t_study_course_record WHERE submitstatus = 2 AND uid=" + uid + ";";
				// alert(sql);
				selectSQL(sql, function(ret, err){
					if (ret.status) {
						// alert('课程学习进度' + JSON.stringify(ret));
						var courseRecords = ret.data;
						if (courseRecords.constructor == Array) {
							// alert(JSON.stringify(courseRecords));
							for (var i = 0; i < courseRecords.length; i++) {
								var courseRecord = courseRecords[i];
								var params = {
									values : {
										uid : uid,
										cid : courseRecord.cid,
										jid : courseRecord.jid,
										// pid : courseRecord.pid,
										vid : courseRecord.vid,
										timenum : courseRecord.timenum,
										crid : courseRecord.crid
										// origtime : courseRecord.origtime,
										// types : courseRecord.types,
										// status : courseRecord.status
									}
								};
								// alert(JSON.stringify(params));
								uploadCourseRecord(params, 'course_record', function(currentParams){
									// var updateStatusSql = "UPDATE t_study_course_record SET submitstatus = 1 WHERE uid=" + currentParams.uid + " AND cid=" + currentParams.cid + " AND crid=" + currentParams.crid + " AND vid=" + currentParams.vid + ";";
									var updateStatusSql = "UPDATE t_study_course_record SET submitstatus = 1 WHERE uid=" + currentParams.uid + " AND cid=" + currentParams.cid + " AND vid=" + currentParams.vid + ";";
									executeSQL(updateStatusSql, function(ret, err){
										if (ret.status) {
											// api.alert({
											//     msg: JSON.stringify(ret)
											// });
										}else{
											// api.alert({
											//     msg: JSON.stringify(err)
											// });
										}
									});
								});
							}
						}
					}
				});
			}
		});
	}
	function searchUploadChapterFailedRecord(){
	
		openDB(function(ret, err){
			if (ret.status) {
				var sql = "SELECT * FROM t_study_course_record_detail WHERE submitstatus = 2 AND uid=" + uid + ";";
				selectSQL(sql, function(ret, err){
					// alert('课程学习详情' + JSON.stringify(ret));
					if (ret.status) {
						var chapterRecords = ret.data;
						if (chapterRecords.constructor == Array) {
							// alert(JSON.stringify(chapterRecords));
							for (var i = 0; i < chapterRecords.length; i++) {
								var chapterRecord = chapterRecords[i];
								var params = {
									values : {
										uid : uid,
										jid : chapterRecord.jid,
										pid : chapterRecord.pid,
										cid : chapterRecord.cid,
										vid : chapterRecord.vid,
										instime : chapterRecord.instime
									}
								};
								// alert(JSON.stringify(params));
								uploadCourseRecord(params, 'chapter_record', function(currentParams){
										var updateStatusSql = "UPDATE t_study_course_record_detail SET submitstatus = 1 WHERE uid=" + currentParams.uid + " AND cid=" + currentParams.cid + " AND vid=" + currentParams.vid + ";";
										executeSQL(updateStatusSql, function(ret, err){
											if (ret.status) {
												// api.alert({
												//     msg: JSON.stringify(ret)
												// });
											}else{
												// api.alert({
												//     msg: JSON.stringify(err)
												// });
											}
										});
									});
							}
						}
					}
				});
			}
		});
	}

	function searchUploadTestAnswerFailed(){
		openDB(function(ret, err){
			if(ret.status){
				var sql = 'select * from test_item where submitstatus=2 and uid='+uid;
				selectSQL(sql, function(ret, err){
					if(ret.status){
						// alert(JSON.stringify(ret.data));
						var testdata = ret.data;
						for (var i = 0; i < testdata.length; i++){
							// var answer = JSON.parse(testdata[i].answer);
							var params = {
									    	tid:testdata[i].testid,
									    	pid:testdata[i].pid,
									    	cid:testdata[i].cid,
									    	chapterid:testdata[i].vid,
									    	stime:testdata[i].stime,
									    	jid:testdata[i].jid,
									    	uid:testdata[i].uid,
									    	xuan:testdata[i].answer
									    }; 

						    uploadTestAnswer(params, function(param){
						    	var updateStatusSql = 'update test_item set submitstatus=1 where uid='+param.uid+' and testid='+param.tid;
						    	executeSQL(updateStatusSql, function(ret, err){
						    		if(ret.status){
						    // 			api.alert({
										//     msg: JSON.stringify(ret)
										// });

						    		}else {
						    // 			api.alert({
										//     msg: JSON.stringify(err)
										// });

						    		}
						    	})
						    })
						}
					}
				})
			}
		});
	}

	function uploadTestAnswer(params, callback){
		
		var requestUrl = Study_SubmitTest;
		api.ajax({
		    url: requestUrl,
		    method: 'post',
		    timeout: 30,
		    dataType: 'json',
		    returnAll: false,
		    data:{
	            values:params
	        }
		},function(ret, err){
			if(ret){
				// if(ret.status == '1'){
					// alert(JSON.stringify(params));
					api.sendEvent({
						name:'studyTestSucceedNotifi',
						extra:{
							chapterid:params.chapterid,
							testid:params.tid,
							pid:params.pid,
							cid:params.cid
						}
					});
					var chapterid = params.chapterid;
					var tid = params.tid;
					var currentparam = {
						uid: uid,
						tid: tid
					};

					openDB(function(ret, err){
						if(ret.status){
							
							var updateTestSql = "UPDATE t_study_test_record SET isanswer=1 WHERE chapterid=" + chapterid + " AND uid=" + uid + ";";
							executeSQL(updateTestSql, function(ret, err){
								if(ret){
									// alert("修改成功!"+ret.status);
									
								}else {
									// alert("修改失败！"+err.msg);
								}
							})

							var updateStatusSql = 'update test_item set submitstatus=1 where uid='+uid+' and testid='+tid;
							executeSQL(updateStatusSql, function(ret, err){
								if(ret){
									// alert("修改成功!"+ret.status);
									callback(currentparam);
									
								}else {
									// alert("修改失败！"+err.msg);
								}
							})

						}
					});
				// }
			}
				
		});
		
	}
	function prepareData(){
		uid = $api.getStorage('cfnetppuid');
		jid = $api.getStorage('cfnetppjxid');
	}